/************************************************************************************
**  
* @copyright (c) 2015-, Technology Co., LTD. All Right Reserved.
*
*************************************************************************************/
/**
* @file     duye_cap_filter.cpp
* @version     
* @brief      
* @author   
* @date     2013-01-21
* @note 
*
*  1. 2018-01-21 created this file
* 
*/
#include <duye_cap_filter.h>

// static const int8* DUYE_LOG_PREFIX = "duye.cap.filter";

namespace duye {

FilterRule::FilterRule() : m_filterWorker(NULL) {}
FilterRule::~FilterRule() {}

void FilterRule::addWhiteRule(const RuleItem& ruleItem) {
	m_whiteRuleList.push_back(ruleItem);
}

void FilterRule::addBlackRule(const RuleItem& ruleItem) {
	m_blackRuleList.push_back(ruleItem);
}

const RuleItemList& FilterRule::whiteRuleList() const {
	return m_whiteRuleList;
}

const RuleItemList& FilterRule::blackRuleList() const {
	return m_blackRuleList;
}

void FilterRule::setFilterWorker(FilterWorker* filterWorker) {
	m_filterWorker = filterWorker;
}

FilterWorker* FilterRule::getFilterWorker() {
	return m_filterWorker;
}

bool FilterRule::hasFilterRule() const {
	bool whiteRuleFlag = false;
	for (RuleItemList::const_iterator iter = m_whiteRuleList.begin(); iter != m_whiteRuleList.end(); ++iter) {
		whiteRuleFlag = iter->hasProtoFilter() || iter->hasIPAddrFilter() || 
			iter->hasRangeIPAddrFilter() || iter->hasPortFilter() || 
			iter->hasRangePortFilter();
	}

	bool blackRuleFlag = false;
	for (RuleItemList::const_iterator iter = m_blackRuleList.begin(); iter != m_blackRuleList.end(); ++iter) {
		blackRuleFlag = iter->hasProtoFilter() || iter->hasIPAddrFilter() 
			|| iter->hasRangeIPAddrFilter() || iter->hasPortFilter()
			|| iter->hasRangePortFilter();
	}

	return whiteRuleFlag || blackRuleFlag;
}

RuleItem::RuleItem() : m_srcIPAddr(0)
	, m_dstIPAddr(0)
	, m_srcPort(0)
	, m_dstPort(0)
	, m_hasProtoFilter(false)
	, m_hasIPAddrFilter(false)
	, m_hasRangeIPAddrFilter(false)
	, m_hasPortFilter(false)
	, m_hasRangePortFilter(false) {}

RuleItem::~RuleItem() {}

void RuleItem::setProtocol(const ProtocolType& protocol) {
	m_hasProtoFilter = true;
	m_protocol = protocol;
}

const ProtocolType& RuleItem::protocol() const {
	return m_protocol;
}

void RuleItem::setSrcIPAddr(const uint32 ip) {
	m_hasIPAddrFilter = true;
	m_srcIPAddr = ip;
}

uint32 RuleItem::srcIPAddr() const {
	return m_srcIPAddr;
}

void RuleItem::setDstIPAddr(const uint32 ip) {
	m_hasIPAddrFilter = true;
	m_dstIPAddr = ip;
}

uint32 RuleItem::dstIPAddr() const {
	return m_dstIPAddr;
}

void RuleItem::setSrcPort(const uint16 port) {
	m_hasPortFilter = true;
	m_srcPort = port;
}

uint16 RuleItem::srcPort() const {
	return m_srcPort;
}

void RuleItem::setDstPort(const uint16 port) {
	m_hasPortFilter = true;
	m_dstPort = port;
}

uint16 RuleItem::dstPort() const {
	return m_dstPort;
}

void RuleItem::setSrcIPRange(const IPRangePair& portRange) {
	m_hasRangeIPAddrFilter = true;
	m_srcIPRange = portRange;
}

const IPRangePair& RuleItem::srcIPRange() const {
	return m_srcIPRange;
}

void RuleItem::setDstIPRange(const IPRangePair& ipRange) {
	m_hasRangeIPAddrFilter = true;
	m_dstIPRange = ipRange;
}

const IPRangePair& RuleItem::dstIPRange() const {
	return m_dstIPRange;
}

void RuleItem::setSrcPortRange(const PortRangePair& portRange) {
	m_hasRangePortFilter = true;
	m_srcPortRange = portRange;
}

const PortRangePair& RuleItem::srcPortRange() const {
	return m_srcPortRange;
}

void RuleItem::setDstPortRange(const PortRangePair& portRange) {
	m_hasRangePortFilter = true;
	m_dstPortRange = portRange;
}

const PortRangePair& RuleItem::dstPortRange() const {
	return m_dstPortRange;
}

bool RuleItem::hasProtoFilter() const {
	return m_hasProtoFilter;
}

bool RuleItem::hasIPAddrFilter() const {
	return m_hasIPAddrFilter;
}

bool RuleItem::hasRangeIPAddrFilter() const {
	return m_hasRangeIPAddrFilter;
}

bool RuleItem::hasPortFilter() const {
	return m_hasPortFilter;
}

bool RuleItem::hasRangePortFilter() const {
	return m_hasRangePortFilter;
}

}
